<script setup lang="ts">
import { onMounted, ref } from "vue";
import { Api } from "../../assets/api/api";
import type { HomeClassifyDataType } from "../../home";
import { useRouter } from "vue-router";

//分类数据
const classifyData = ref<Array<HomeClassifyDataType>>([]);
const page = ref("");
const ranking = ref("");
const classify = ref("");
const boy = ref("");
const girl = ref("");
const classifyIndex = ref(0);

// vue3自带方法
const router = useRouter();

const classifyList = async () => {
  let { data } = await Api.gEtHomeNavUrl();
  classifyData.value = data.data.navi;

  classifyData.value.splice(5, 1);
  let boy1 = classifyData.value[2];
  let girl1 = classifyData.value[3];
  classifyData.value.splice(2, 1);
  classifyData.value.splice(2, 1);
  classifyData.value.push(boy1);
  classifyData.value.push(girl1);
  classifyData.value[0].name = "首页";
  classifyData.value[1].name = "排行";
  classifyData.value[2].name = "分类";
  page.value = classifyData.value[0].name;
  ranking.value = classifyData.value[1].name;
  classify.value = classifyData.value[2].name;
  boy.value = classifyData.value[3].name;
  girl.value = classifyData.value[4].name;
};
//跳转首页兄弟页面
const goView = (i: number) => {
  classifyIndex.value = i;
  if (i == 0) {
    router.push("/");
  } else if (i == 1) {
    router.push("/ranking");
  } else if (i == 2) {
    router.push("/classification");
  } else if (i == 3) {
    router.push("/boy");
  } else if (i == 4) {
    router.push("/girl");
  }
};
// 跳转书架页面
const goBookshelfView = ()=>{
  router.push('/bookshelfv');
}

onMounted(() => {
  classifyList();
});
</script>

<template>
  <header>
    <!-- 标题 -->
    <nav>
      <div class="main">
        <van-image width="17" height="17" src="/img/home/apply.png" />
      </div>
      <h1>天浪小说</h1>
      <div class="sourch">
        <p @click="goBookshelfView">书架</p>
        <div class="img" @click="router.push('/search')"></div>
      </div>
    </nav>
    <!-- 分类 -->
    <ul class="classify">
      <li class="li" :class="{ con: classifyIndex == 0 }" @click="goView(0)">
        {{ page }}
      </li>
      <li class="li" :class="{ con: classifyIndex == 1 }" @click="goView(1)">
        {{ ranking }}
      </li>
      <li class="li" :class="{ con: classifyIndex == 2 }" @click="goView(2)">
        {{ classify }}
      </li>
      <li class="li" :class="{ con: classifyIndex == 3 }" @click="goView(3)">
        {{ boy }}
      </li>
      <li class="li" :class="{ con: classifyIndex == 4 }" @click="goView(4)">
        {{ girl }}
      </li>
    </ul>
  </header>

  <router-view></router-view>

  <!-- 搜索 -->
  <div class="seek">
    <input type="text" placeholder="作者名/书名" />
    <div class="seek_btn"></div>
  </div>
  <br /><br /><br />

  <footer>
    <p>©2022天浪小说</p>
    <!-- <p>京ICP备 11009265号-5</p> -->
  </footer>
  <br />
</template>

<style lang="scss" scoped>
nav {
  display: flex;
  justify-content: space-between;
  background-color: #f7f7f7;
  border-bottom: 1px solid #cbcbcb;
  .main {
    width: 17px;
    height: 17px;
    margin-top: 13px;
    padding-left: 11px;
    padding-right: 44px;
  }
  h1 {
    font-size: 17px;
    color: #333333;
    padding-top: 12px;
    padding-bottom: 10px;
    font-weight: normal;
  }
  .sourch {
    display: flex;
    width: 60px;
    padding-right: 12px;
    justify-content: space-between;
    align-items: center;
    p {
      font-size: 14px;
      color: #5f5f5f;
    }
    .img {
      width: 16px;
      height: 16px;
      background: url(/public/img/home/search.png) no-repeat center center;
      background-size: cover;
    }
  }
}
.classify {
  display: flex;
  height: 34px;
  justify-content: space-between;
  background-color: #eeeeee;
  border-bottom: 1px solid #dadada;
  .li {
    width: 75px;
    height: 32px;
    display: block;
    color: #5f5f7f;
    font-size: 14px;
    text-align: center;
    line-height: 32px;
    &.con {
      border-bottom: 2px solid #f54b41;
      color: #f54b41;
    }
  }
}
.seek {
  display: flex;
  width: 353px;
  height: 33px;
  border: 1px solid #d2d6d8;
  margin: 0 auto;
  .seek_btn {
    width: 42px;
    background: #fff url(/public/img/home/search2.png) no-repeat center center;
    background-size: 16px;
    border-left: 1px solid #d2d6d8;
  }
  input {
    width: 300px;
    padding-left: 10px;
    border: none;
    background-color: #fff;
  }
}
footer {
  p {
    text-align: center;
    font-size: 12px;
    color: #999999;
    margin-bottom: 10px;
  }
}
</style>